package it.polimi.swim.servlet;

import it.polimi.swim.entity.Skill;
import it.polimi.swim.session.SkillManagerBeanRemote;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AvailableSkillsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Context ctx;

	public AvailableSkillsServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try {
			String err = (String) request.getAttribute("errorMessage");
			System.out.println(err);
			request.setAttribute("errorMessage", request.getAttribute("errorMessage"));
			String path = request.getParameter("path");
			
			System.out.println(path);
			
			List<Skill> skillList = new ArrayList<Skill>();

			// Inizializzo contesto
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			// Richiamo il session bean
			ctx = new InitialContext();
			Object ref = ctx.lookup("SkillManagerBean/remote");
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref;

			// Richiamo availableSkill del session
			skillList = skillManager.availableSkills();

			request.setAttribute("skillList", skillList);
			request.getRequestDispatcher(path).forward(
					request, response);

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		

	}

}
